//
//  NetWorkTools.swift
//  Webo
//
//  Created by 曾静 on 16/6/26.
//  Copyright © 2016年 qq. All rights reserved.
//


import UIKit
import AFNetworking

enum MethodType:String{
    case GET = "GET"
    case POST = "POST"
}

//swift的区别,返回数据的格式以及请求数据的格式都是系统自己动态决定的,不用自己手动添加
//_instance.responseSerializer = [AFHTTPResponseSerializer serializer];:例如

class ZJNetWorkTools: AFHTTPSessionManager {
    
    static let shareTools: ZJNetWorkTools = {
        
    let shareTools = ZJNetWorkTools()
        
    shareTools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
    return shareTools
        
    }()
    
     typealias callBackType = (response:AnyObject?,error:NSError?)->()
    
    func request(type:MethodType,params:AnyObject?,urlString:String,callBack:callBackType){
        
        let sucess = { (datatask:NSURLSessionDataTask, response:AnyObject?) -> Void in
            
            callBack(response: response, error: nil)
            
            }
        let fail =  {(datatask:NSURLSessionDataTask?, error: NSError) -> Void in
            callBack(response: nil, error: error)
        }
        
        if type == .GET {
            
            GET(urlString, parameters: params, progress: nil, success: sucess, failure: fail)
            
            
        }else{
            
             POST(urlString, parameters: params, progress: nil, success: sucess, failure: fail)
            
        }
        
        
    }
    

}

extension ZJNetWorkTools{
    
    //获取token的接口
    func getToken(code: String, callBack: callBackType){
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let params = [
            
            "client_id": APPKEY,
            "client_secret": APPSECRET,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": REDIRECT_URI
        ]
        
       request(.POST, params: params, urlString: urlString, callBack: callBack)
        
        
    }
    
    
    //获取用户信息的接口
    
    func getUserAccount(userAccount:ZJUserAccount,callBack: callBackType){
        
        //https://api.weibo.com/2/users/show.json
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        //  设置参数
        let params = [
            
            "access_token": userAccount.access_token!,
            
            "uid": userAccount.uid!
        ]
       
        request(.GET, params: params, urlString: urlString, callBack: callBack)
    }
    
    //获取首界面的信息
    func getHomeData(sinceId:Int64,maxId:Int64,callBack: callBackType){
        
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        //  设置参数
        let params = [
            "since_id":"\(sinceId)",
            "max_id":"\(maxId)",
            "access_token": ZJUserInfoTools.shareUserInfo.token!
        ]
        
        request(MethodType.GET, params: params, urlString: url, callBack: callBack)

    }
    
    //发布微博的接口
    func updateUserData(status:String,callBack: callBackType){
        
        
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        //  设置参数
        let params = [
            "status":status,
            "access_token": ZJUserInfoTools.shareUserInfo.token!
        ]

        request(.POST, params: params, urlString: urlString, callBack: callBack)
        
        
    }
    
    //发布一条带图片的微博的接口

    func uploadUserDataWithOnePicture(status:String,imageList:[UIImage],sucess:((AnyObject?)->()) ,fail:((NSError)->())){
        
        
        let urlString = "https://upload.api.weibo.com/2/statuses/upload.json"
        //  设置参数
        let params = [
            "status":status,
            "access_token": ZJUserInfoTools.shareUserInfo.token!
        ]

        POST(urlString, parameters: params, constructingBodyWithBlock: { (formData) -> Void in
            
            
            for image in imageList{
                
                let data = UIImagePNGRepresentation(image)!
                
                 formData.appendPartWithFileData(data, name: "pic", fileName: "oooo", mimeType: "application/octet-stream")
                
            }
            
            
            
            
            }, progress: nil, success: { (_, response) -> Void in
                
                sucess(response)
                
            }) { (_, error) -> Void in
                
                fail(error)
        }
        
    }
    
    
    
    
    
    
}
